<?php
//add dragon.hu 2015.12.23 businessmanagerrole

defined('BASEPATH') or die('forbidden');

class Businessmanagerrole_Controller extends Controller
{
	private $conn;
	private $accountconn;
	private $startdate;
	private $channel;
	private $channelList;  //渠道列表
	private $areanum;
	private $opentime;    //开服时间
	
	function _init()
	{
		check_level(50, segment(0), segment(1));
		set_time_limit(0);
		
		$this->areanum = $_SESSION['area_selected_id'];
		if ($this->areanum == '')
		{
			echo "<script>alert('".translate('C_SELECT_AREA_TEXT')."');</script>";
			return;
		}
		//$this->areaList = get_area_list_option();
		$this->channelList = array(
				'0' => '渠道无关', 'uc' => 'UC', 'baidu' => 'Baidu', 'sanliuling' => '360',
				'xiaomi' => 'Xiaomi', 'lianxiang' => 'Lianxiang', 'oppo' => 'Oppo', 'huawei' => 'Huawei'
		);
		$sql = "SELECT opentime FROM tt_areasample WHERE id = $this->areanum";
		$one = $this->db->one($sql);
		$this->opentime = $one['opentime'];
		
		//$this->accountconn = $this->load->database('center_db');
		
		//$this->conn = getTmpDbConnect(PLATFORM_ALL, 'all', $this->areanum, DBTYPE_LOG);
		//$this->conn = $this->load->database('log_db');
		$this->conn = getTmpDbConnect(PLATFORM_ALL, 'all', $this->areanum, DBTYPE_LOG);
	}
	
	function index()
	{
		if(is_submit())
		{
			$this->startdate = $_POST['row']['starttime'];
			//$enddate = $_POST['row']['endtime'];
			//$serveridtmp = $_POST['row']['area'];
			//$tmp = explode("-", $serveridtmp);
			//$serverid = $tmp[0];
			$this->channel = /*$_POST['row']['channel']*/0;  //分区上无法查到账号服的log
			//var_dump($_POST);
			//exit();
			
		}else
		{
			$this->startdate = date("Y-m-d", $this->opentime);  //默认开服当天
			$this->channel = 0;  //默认渠道无关
			//$serverid = $this->areanum;
		}
		if (empty($this->areanum) || empty($this->conn))
		{
			echo "<script>alert('areanum empty or connect database failed!!!');</script>";
			return;
		}
		$remainData = array();
		//今日注册人账号ID数组
		//$this->channel == '0' ? $sql = "SELECT id FROM account WHERE creat_time like '".$this->startdate.'%'."'" : $sql = "SELECT id FROM account WHERE creat_time like '".$this->startdate.'%'."' and platform = '".$this->channel."'";
		//今日注册的角色ID数组
		$this->channel == '0' ? $sql = "SELECT rolecreate.role_id FROM rolecreate where operation_time like '".$this->startdate.'%'.
		"'" : $sql = "SELECT rolecreate.role_id FROM rolecreate left JOIN accountcreate ac on rolecreate.account = ac.account where rolecreate.operation_time like '".$this->startdate.'%'."' and ac.platform_id = '".$this->channel."'";
		$this->conn->query($sql);
		$starttimeList = array();
		while ($n = $this->conn->get_array('default'))
		{
			$starttimeList[] = $n['role_id'];
		}
		
		
		for($i = 0; $i < 31; $i++)  //设定为一个月
		{
			//1.当天注册角色数
			$tmpdate = date("Y-m-d", (strtotime($this->startdate) + $i * 86400));
			$this->channel == '0' ? $sql = "SELECT COUNT(*) AS num FROM rolecreate WHERE operation_time like '".$tmpdate.'%'.
			"'" : $sql = "SELECT COUNT(*) AS num FROM rolecreate left JOIN accountcreate ac on rolecreate.account = ac.account WHERE rolecreate.operation_time like '".$tmpdate.'%'."' and ac.platform_id = '".$this->channel."'";
			//var_dump($this->channel == '0');
			//var_dump($sql);
			$one = $this->conn->one($sql);
			$remainData[$tmpdate]['todayregisternum'] = $one['num'];
			
			//2.当天玩家登录总数
			//$sql2 = "SELECT COUNT(DISTINCT(account_id)) AS num2 FROM accountlogin WHERE login_time like '".$tmpdate.'%'."'";
			//$one2 = $this->conn->one($sql2);
			//$remainData[$tmpdate]['todayloginnum'] = $one2['num2'];
			
			//2.当天角色登录数组
			$targetdayList = array();
			$this->channel == '0' ? $sql2 = "SELECT DISTINCT(role_id) FROM rolelogin WHERE operation_time like '".
			$tmpdate.'%'."'" : $sql2 = "SELECT DISTINCT(role_id) FROM rolelogin left JOIN accountcreate ac on rolelogin.account = ac.account WHERE rolelogin.operation_time like '".$tmpdate.'%'."' and ac.platform_id = '".$this->channel."'";
			$this->conn->query($sql2);
			while ($n = $this->conn->get_array('default'))
			{
				$targetdayList[] = $n['role_id'];
			}
			$remainData[$tmpdate]['todayloginnum'] = count($targetdayList);
			
			$tmparr = array_diff($starttimeList, $targetdayList);
			//3.当天留存的开服日角色数
			$remainData[$tmpdate]['todayremainfirstdaynum'] = count($starttimeList) - count($tmparr);
			//当天留存率
			$remainData[$tmpdate]['remainrate'] = round(($remainData[$tmpdate]['todayremainfirstdaynum'])/count($starttimeList), 4) * 100 ."%";
		}
		//var_dump($remainData);
		//exit();
		
		$this->view->assign('startdate', $tmpdate);
		//$this->view->assign('enddate', $enddate);
		//$this->view->assign('areaList', $this->areaList);
		$this->view->assign ("channel", $this->form->select ("row[channel]", $this->channelList, '', array('id' => 'channel')));
		$this->view->assign('remainData', $remainData);
		
		$this->view->display("businessdata/businessmanagerrole/index");
	}
	
}









?>